package com.zycfc.zsf.boot.exception.config;

import org.springframework.boot.context.properties.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.web.servlet.error.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
import com.zycfc.zsf.boot.exception.handler.*;
import com.zycfc.zsf.boot.exception.error.*;
import org.springframework.boot.web.servlet.error.*;
import org.springframework.boot.autoconfigure.condition.*;
import org.slf4j.*;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({ ExceptionProperties.class })
@AutoConfigureBefore({ ErrorMvcAutoConfiguration.class })
public class ExceptionAutoConfiguration
{
    private static final Logger log;
    @Autowired
    private ExceptionProperties properties;
    
    @Bean
    public ZsfExceptionHandler zsfExceptionHandler() {
        return new ZsfExceptionHandler(this.properties);
    }
    
    @Bean
    @ConditionalOnProperty(value = { "zsf.boot.exception.enabled" }, matchIfMissing = true)
    public GlobalExceptionHandler zsfGlobalExceptionHandler(final ZsfExceptionHandler sxfExceptionHandler) {
        ExceptionAutoConfiguration.log.info("^_^\u5168\u5c40\u5f02\u5e38\u62e6\u622a\u7ec4\u4ef6\u52a0\u8f7d\u6210\u529f^_^");
        return new GlobalExceptionHandler(sxfExceptionHandler);
    }
    
    @Bean
    @ConditionalOnProperty(value = { "zsf.boot.exception.overriddenErrorController" }, matchIfMissing = false)
    @ConditionalOnMissingBean(value = { ErrorController.class }, search = SearchStrategy.CURRENT)
    public ErrorEndpoint errorEndpoint() {
        return new ErrorEndpoint();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ExceptionAutoConfiguration.class);
    }
}
